Imports DotNetNuke.Entities.Portals
Imports DnnSecurity.Results
Imports System.Security

Namespace Checks
    Public Class WebAppTrustLevel
        Inherits MultilingualBase
        Implements IDnnSecurityCheck


        Public ReadOnly Property DisplayName As String Implements IDnnSecurityCheck.DisplayName
            Get
                Return GetTranslationFor (Me.GetType.Name & ".DisplayName")
            End Get
        End Property

        Private _ShortInfoText As String

        Public ReadOnly Property ShortInfoText() As String Implements IDnnSecurityCheck.ShortInfoText
            Get
                Return GetTranslationFor (Me.GetType.Name & ".ShortInfoText")
            End Get
        End Property

        Public ReadOnly Property Description() As String Implements IDnnSecurityCheck.Description
            Get
                Return GetTranslationFor (Me.GetType.Name & ".Description")
            End Get
        End Property

        Public ReadOnly Property SecurityRiskClassification As SecurityRiskClassification _
            Implements IDnnSecurityCheck.SecurityRiskClassification
            Get
                Dim res As IDnnSecurityResult =
                        Results.OrderByDescending (Function(x) x.SecurityRiskClassification).FirstOrDefault
                If res IsNot Nothing Then
                    Return res.SecurityRiskClassification
                End If

                Return SecurityRiskClassification.Ok
            End Get
        End Property

        Private _results As New List(Of IDnnSecurityResult)

        Public ReadOnly Property Results() As List(Of IDnnSecurityResult) Implements IDnnSecurityCheck.Results
            Get
                Return _results
            End Get
        End Property

        Public Sub Execute (ByVal portalSettings As PortalSettings, ByVal result As IAsyncResult) _
            Implements IDnnSecurityCheck.Execute
            Dim trustLevel As AspNetHostingPermissionLevel = GetCurrentTrustLevel()
            _ShortInfoText = GetCurrentTrustLevel().ToString ("g")

            If trustLevel > AspNetHostingPermissionLevel.Medium Then
                _results.Add (New FullTrust)
            End If
        End Sub

        Public Overloads Sub LoadRessourceFile (ByVal fileName As String) Implements IDnnSecurityCheck.LoadRessourceFile
            MyBase.LoadRessourceFile (fileName)
        End Sub

        Private Function GetCurrentTrustLevel() As AspNetHostingPermissionLevel

            For Each trustLevel As AspNetHostingPermissionLevel In New _
                AspNetHostingPermissionLevel() {AspNetHostingPermissionLevel.Unrestricted,
                                                AspNetHostingPermissionLevel.High, AspNetHostingPermissionLevel.Medium,
                                                AspNetHostingPermissionLevel.Low, AspNetHostingPermissionLevel.Minimal}

                Try
                    Dim HostedPermission As AspNetHostingPermission = New AspNetHostingPermission (trustLevel)
                    HostedPermission.Demand()
                Catch generatedExceptionName As SecurityException
                    Continue For
                End Try

                Return trustLevel
            Next

            Return AspNetHostingPermissionLevel.None
        End Function
    End Class
End Namespace

